function [subdata] = subsample(data,fraction)
% Selects a subsample of data with a fraction of the original fields.
% [subdata] = subsample(data,fraction)
% inputs: data     -> data set with variable FID_Grid with the identifier for field
%         fraction -> fraction of fields to keep (between 0 and 1)
% output: subdata  -> original dataset with randomly selected fields.

% Fields:
fields = unique(data.FID_Grid);
n      = length(fields);
k      = floor(fraction*n);

p = randperm(n,k);

subdata = data(p,:);

% new_fields = fields(p);
% 
% subdata_idx = [];
% idx         = 1:size(data,1);
% 
% for nf = new_fields'
%     subdata_idx = [subdata_idx;idx(data.FID_Grid==nf)'];
% end
% 
% subdata = data(subdata_idx,:);
    